--滑动盘
local Controller = MClass("Controller")

function Controller:ctor()
	--self.root 默认为根节点
	self.dirControllerNode = self.root:findChild("Panel")
    self.dirTrackBg = self.dirControllerNode:findChild("radius")
    self.dirTrackBgImg = self.dirTrackBg:findChild("bg")
    self.btnLeft = self.dirTrackBg:findChild("btn_l")
    self.btnRight = self.dirTrackBg:findChild("btn_r")

	self:init()
end

function Controller:init()
    self.dirTarckBgInitPosX, self.dirTarckBgInitPosY = self.dirTrackBgImg:getPosition()
    self.currentManualMoveFlag = Enums.MoveFlag.stop
    -- body
    self:addTouch()

     --add onEnter onExit
    local function onNodeEvent(event)
        if "enter" == event then
        elseif "exit" == event then
            cc.Director:getInstance():getEventDispatcher():removeEventListener(self.listener)
        end
    end
    self:registerScriptHandler(onNodeEvent)

    self.lastClickTime = 0
end

function Controller:check(pos,panel)
    local boundingBox = panel:getBoundingBox()
    if cc.rectContainsPoint(boundingBox,pos) then
        return true
    else
        return false
    end
end

function Controller:updateFrame()
    BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_CANCEL_AUTO)
end

function Controller:addTouch()
	 --dir controller
    local function dirController(pos)
        local x = pos.x - self.dirTarckBgInitPosX
        local moveFlag
        if x > 0 then
            moveFlag = Enums.MoveFlag.right
            self.btnRight:setScale(1.1)
            self.btnLeft:setScale(1.0)
            --print("right")
        else
            moveFlag = Enums.MoveFlag.left
            self.btnLeft:setScale(1.1)
            self.btnRight:setScale(1.0)
            --print("left")
        end

        if moveFlag ~= self.currentManualMoveFlag then
        	BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.PLAYER_MOVE,{name = BattleUIEvent.TYPE.PLAYER_MOVE,moveFlag = moveFlag})
            self.currentManualMoveFlag = moveFlag
            --print("currentManualMoveFlag",self.currentManualMoveFlag)
            self:performWithDelay(function()
                BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.PLAYER_MOVE,{name = BattleUIEvent.TYPE.PLAYER_MOVE,moveFlag = self.currentManualMoveFlag})
            end,0.05)
        end
    end

    local function touchEvent_Dir(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            local p = sender:getTouchBeganPosition()
            p = self.dirTrackBg:convertToNodeSpace(p)
            dirController(p)
            BattleUIEvent:addObjToFrameUpdate(self,Controller.updateFrame)
        elseif eventType == ccui.TouchEventType.moved then
            local p = sender:getTouchMovePosition()
            p = self.dirTrackBg:convertToNodeSpace(p)
            dirController(p)
        elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then
            BattleUIEvent:removeFromUpdate(self)
            self.btnLeft:setScale(1)
            self.btnRight:setScale(1)
            BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.PLAYER_MOVE,{name = BattleUIEvent.TYPE.PLAYER_MOVE,moveFlag = Enums.MoveFlag.stop})
            BattleUIEvent:dispatchEvent(BattleUIEvent.TYPBATTLE_CANCEL_AUTOTO)
            if SKILL_ROLL_ENABLED then
                if self.currentManualMoveFlag == self.lastClickDir then
--                    print("========> double click delta:", app:getUTCTime() - self.lastClickTime)
                    if app:getUTCTime() - self.lastClickTime <= Constant.BattleRollDoubleTapInterval then
                        BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.PLAYER_ROLL,{})
                    else

                    end
                    self.lastClickDir = nil
                else
                    self.lastClickDir = self.currentManualMoveFlag
                end
                self.lastClickTime = app:getUTCTime()
            end
            self.currentManualMoveFlag = Enums.MoveFlag.stop
        end
    end
    self.dirControllerNode:addTouchEventListener(touchEvent_Dir)

    local platform = cc.Application:getInstance():getTargetPlatform()
    if platform == cc.PLATFORM_OS_WINDOWS or platform == cc.PLATFORM_OS_MAC then
        local function refreshKeyPressed()
            if self._leftkeypressed then
                dirController(cc.p(-9999,0))
            elseif self._rightkeypressed then
                dirController(cc.p(9999,0))
            else
--                self.dirTrackBgImg:setPosition(self.dirTarckBgInitPosX,self.dirTarckBgInitPosY)
                self.btnLeft:setScale(1)
                self.btnRight:setScale(1)
                BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.PLAYER_MOVE,{name = BattleUIEvent.TYPE.PLAYER_MOVE,moveFlag = Enums.MoveFlag.stop})
                BattleUIEvent:dispatchEvent(BattleUIEvent.TBATTLE_CANCEL_AUTOAUTO)
                if SKILL_ROLL_ENABLED then
                    if self.currentManualMoveFlag == self.lastClickDir then
--                        print("========> double click delta:", app:getUTCTime() - self.lastClickTime)
                        if app:getUTCTime() - self.lastClickTime <= Constant.BattleRollDoubleTapInterval then
                            BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.PLAYER_ROLL,{})
                        else

                        end
                        self.lastClickDir = nil
                    else
                        self.lastClickDir = self.currentManualMoveFlag
                    end
                    self.lastClickTime = app:getUTCTime()
                end
            end
        end

        local function keyboardPressed(keyCode, event)
            if keyCode == 124 then
                BattleUIEvent:addObjToFrameUpdate(self,Controller.updateFrame)
                self._leftkeypressed = true
                dirController(cc.p(-9999,0))
            elseif keyCode == 127 then
                BattleUIEvent:addObjToFrameUpdate(self,Controller.updateFrame)
                self._rightkeypressed = true
                dirController(cc.p(9999,0))
            elseif keyCode == 146 or keyCode == 89 then --w + s -
                MapManager:mapTouchZoomIn(0.03,0.1)
            elseif keyCode == 142 or keyCode == 73 then
                MapManager:mapTouchZoomOut(0.03,0.1)
            end
        end

        local function keyboardReleased(keyCode, event)
            if keyCode == 124 then
                self._leftkeypressed = false
                refreshKeyPressed()
                BattleUIEvent:removeFromUpdate(self)
            elseif keyCode == 127 then
                self._rightkeypressed = false
                refreshKeyPressed()
                BattleUIEvent:removeFromUpdate(self)
            end
            self.currentManualMoveFlag = Enums.MoveFlag.stop
        end
        local listener = cc.EventListenerKeyboard:create()
        listener:registerScriptHandler(keyboardPressed, cc.Handler.EVENT_KEYBOARD_PRESSED)
        listener:registerScriptHandler(keyboardReleased, cc.Handler.EVENT_KEYBOARD_RELEASED)
        local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
        eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self.dirControllerNode)
        self.listener = listener
    end
end

return Controller
